import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import('../views/Login');
const MyHost = () => import('../views/MyHost/MyHost');
const Home = () => import('../views/Home');
const Index = () => import('../views/Index/Index');
const OrderList = () => import('../views/OrderList/OrderList');
const AllOrderList = () => import('../views/AllOrderList/AllOrderList');
const WorkerList = () => import('../views/WorkerList/WorkerList');
const MoneyList = () => import('../views/MoneyList/MoneyList');
const ChartView = () => import('../views/ChartView/ChartView');
const NotFound = () => import('../views/NotFound');
const UserList = () => import('../views/UserList/UserList');
const FinishOrder = () => import('../views/OrderManage/Finish');
const RollbackOrder = () => import('../views/OrderManage/Rollback');

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: { label: '登录' }
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    redirect: '/home/index',
    children: [
      {
        path: 'index',
        name: 'index',
        component: Index,
        meta: { label: '数据总览' }
      },
      {
        path: 'order-list',
        name: 'order-list',
        component: OrderList,
        meta: { label: '订单列表' }
      },
      {
        path: 'all-list',
        name: 'all-list',
        component: AllOrderList,
        meta: { label: '订单统计' }
      },
      {
        path: 'worker-list',
        name: 'worker-list',
        component: WorkerList,
        meta: { label: '人员列表' }
      },
      {
        path: 'money-list',
        name: 'money-list',
        component: MoneyList,
        meta: { label: '提成管理' }
      },
      {
        path: 'chart-view',
        name: 'chart-view',
        component: ChartView,
        meta: { label: '图表查看' }
      },
      {
        path: 'my-host',
        name: 'my-host',
        component: MyHost,
        meta: { label: '我的主页' }
      },
      {
        path: 'user-list',
        name: 'user-list',
        component: UserList,
        meta: { label: '用户列表' }
      },
      {
        path: 'order/finish',
        name: 'order-finish',
        component: FinishOrder,
        meta: { label: '已完成订单' }
      },
      {
        path: 'order/rollback',
        name: 'order-rollback',
        component: RollbackOrder,
        meta: { label: '已退回订单' }
      }
    ]
  },
  {
    path: '*',
    name: '404',
    component: NotFound,
    meta: { label: '404' }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

router.beforeEach((to, from, next) => {
  if (to.name === 'login') {
    next();
  } else {
    const token = localStorage.getItem('access_token');
    if (!token || token === '') next({ name: 'login' });
    else next();
  }
})

export default router
